#region Licence
// Copyright (c) 2008 James Gregory (james@jagregory.com)
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//     * Neither the name of James Gregory nor the names of its
//     contributors may be used to endorse or promote products derived from this
//     software without specific prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion

using System;
using System.Collections.Generic;
using Boo.Lang.Compiler.Ast;
using Boo.Lang.Compiler.Steps;
using JAGregory.BooPlugin.Language.Members;

namespace JAGregory.BooPlugin.Language
{
    class MemberLookupVisitor : AbstractTransformerCompilerStep
    {
        private readonly List<MemberDeclaration> members = new List<MemberDeclaration>();
        private Node target;
        private int line;
        private int column;

        public override void Run()
        {
            target = null;
            Visit(CompileUnit);
        }

        public override void OnMemberReferenceExpression(MemberReferenceExpression node)
        {
            base.OnMemberReferenceExpression(node);
        }
        
        public override void OnDeclaration(Declaration node)
        {
            InScope(node);
            base.OnDeclaration(node);
        }

        public override void OnField(Field node)
        {
            InScope(node);
            base.OnField(node);
        }

        public override void OnProperty(Property node)
        {
            InScope(node);
            base.OnProperty(node);
        }

        public override void OnDoubleLiteralExpression(DoubleLiteralExpression node)
        {
            InScope(node);
            base.OnDoubleLiteralExpression(node);
        }

        public override void OnReferenceExpression(ReferenceExpression node)
        {
            InScope(node);

            Node parent = node;

            // work up tree to get top level namespace reference
            while ((parent = parent.ParentNode).NodeType == NodeType.MemberReferenceExpression)
            {
                InScope(parent);
            }

            base.OnReferenceExpression(node);
        }

        public override void OnStringLiteralExpression(StringLiteralExpression node)
        {
            InScope(node);
            base.OnStringLiteralExpression(node);
        }

        public override void OnCharLiteralExpression(CharLiteralExpression node)
        {
            InScope(node);
            base.OnCharLiteralExpression(node);
        }

        public override void OnBoolLiteralExpression(BoolLiteralExpression node)
        {
            InScope(node);
            base.OnBoolLiteralExpression(node);
        }

        public override void OnEnumMember(EnumMember node)
        {
            InScope(node);
            base.OnEnumMember(node);
        }

        public override void OnCallableTypeReference(CallableTypeReference node)
        {
            InScope(node);
            base.OnCallableTypeReference(node);
        }

        protected override void OnError(Node node, Exception error)
        {
            throw new NotImplementedException("The method or operation is not implemented.");
        }

        public override void OnNamespaceDeclaration(NamespaceDeclaration node)
        {
            InScope(node);
            base.OnNamespaceDeclaration(node);
        }

        private void InScope(Node node)
        {
            if (node.LexicalInfo.Line <= Line && node.LexicalInfo.Column <= Column && IsCloser(node))
                target = node;
        }

        private bool IsCloser(Node node)
        {
            if (Target == null)
                return true;

            return (node.LexicalInfo.Line >= Target.LexicalInfo.Line &&
                    node.LexicalInfo.Column >= Target.LexicalInfo.Column);
        }

        public Node Target
        {
            get { return target; }
        }

        public List<MemberDeclaration> Members
        {
            get { return members; }
        }

        public int Line
        {
            get { return line; }
            set { line = value; }
        }

        public int Column
        {
            get { return column; }
            set { column = value; }
        }
    }
}